home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / virtmem.exe / VIRTUEXM.PAS < prev    next >
Pascal/Delphi Source File  |  1992-10-09  |  5KB  |  148 lines

  1. {VirtuExm}
  2. {Program to demonstrate the use of VirtuMem.}
  3. {This program builds a linked list with random numbers, a record number,
  4.  and wordS from a test sentence. (Each element of the linked list has one
  5.  word.)  It then sets the element at the head of the list to have the
  6.  lowest number of those randomly generated.  Following that it frees the
  7.  linked list element by element, printing as it goes.}
  8. {This program was written by Wayne Knorr.}
  9. {Program start date:  October 7, 1992.}
  10. {$DEFINE USEEMS}
  11. {$DEFINE COMPUSERVE}
  12. Program VirtuExm;
  13.  
  14. Uses VirtuMem,Secondar;
  15.  
  16. Type
  17.  
  18.      ExamplePtr=^ExampleRec;
  19.      ExampleRec=
  20.         Record
  21.           RecNum     :Integer;
  22.           RndNum     :Word;
  23.           EnglishWord:String;
  24.           Next       :LongInt{ExamplePtr};
  25.         End;
  26.  
  27. Var
  28.  
  29.      ExampleHead :LongInt{Virtual ExamplePtr};
  30.      CurExample  :LongInt{Virtual ExamplePtr};
  31.      CurRecordNum:Byte;
  32.      TestSentence:String;
  33.  
  34. {--------------------}
  35. {ExamplePtrR}
  36. {Virtual deferencing for ExampleRec types.  This routine is useful because
  37.  it saves the need to typecast pointers returned by the routine R.}
  38. Function ExamplePtrR(   Virt :LongInt;
  39.                         Mucky:Integer):ExamplePtr;
  40.  
  41. Var
  42.  
  43.      Temp:ExamplePtr;                  {It is useful to define a temporary
  44.                                         variable and do the conversion in
  45.                                         two lines so the structure may be
  46.                                         more easily looked at while
  47.                                         debugging.}
  48.  
  49. BEGIN
  50.  
  51.   Temp:=R(Virt,Mucky);                 {Reference the virtual pointer, putting it in real memory}
  52.   ExamplePtrR:=ExamplePtr(Temp);       {Type cast to appropriate pointer type}
  53.  
  54. END;
  55. {--------------------}
  56. {LMin}
  57. {Compares two Words, copying the least of the two into the first.}
  58. Procedure LMin(var FirstLong :Word;
  59.                    SecondLong:Word);
  60.  
  61. BEGIN
  62.  
  63.   If (SecondLong<FirstLong) then
  64.        FirstLong:=SecondLong;
  65.  
  66. END;
  67. {====================}
  68. bEGIN {Main program.}
  69.  
  70.   Randomize;
  71.   OpenBook(FALSE);                     {Start VirtuMem, allowing EMS to be used}
  72.   TestSentence:='A dog and a cat ran down the street.';
  73.   CurRecordNum:=1;
  74.  
  75.   {Build the linked list from tail to head.}
  76.   ExampleHead:=Null;
  77.   While (Pos(' ',TestSentence)<>0) do  {One dynamic record for each word in the test sentence}
  78.      Begin
  79.        CurExample:=ANew(SizeOf(ExampleRec));
  80.        With ExamplePtrR(CurExample,Stay)^ do
  81.           Begin
  82.             RecNum:=CurRecordNum;
  83.             RndNum:=Random(MaxInt);
  84.             EnglishWord:=Copy(TestSentence,1,Pos(' ',TestSentence)-1);        {Extract a word and truncate the sentence}
  85.             TestSentence:=Copy(TestSentence,Pos(' ',TestSentence)+1,255);
  86.             Next:=ExampleHead;
  87.           End;
  88.        Unstay(CurExample);
  89.        ExampleHead:=CurExample;
  90.        CurRecordNum:=CurRecordNum+1;
  91.      End;
  92.   {Pick up the last word.}
  93.   CurExample:=ANew(SizeOf(ExampleRec));
  94.   ExamplePtrR(CurExample,Dirt)^.RecNum:=CurRecordNum;
  95.   ExamplePtrR(CurExample,Dirt)^.RndNum:=Random(MaxInt);
  96.   ExamplePtrR(CurExample,Dirt)^.EnglishWord:=TestSentence;
  97.   TestSentence:='';
  98.   ExamplePtrR(CurExample,Dirt)^.Next:=ExampleHead;
  99.   ExampleHead:=CurExample;
  100.   CurRecordNum:=CurRecordNum+1;
  101.  
  102.   {Do a simple test involving double dereferencing.}
  103.   If ((ExamplePtrR(ExamplePtrR(CurExample,Clen)^.Next,Clen)^.RndNum mod 2)=0)
  104.     then
  105.        WriteLn('The second link in the list has an even random number, ',
  106.          ExamplePtrR(ExamplePtrR(CurExample,Clen)^.Next,Clen)^.RndNum,'.' );
  107.  
  108.   {Find the lowest random number in the linked list and make the head's
  109.    random number equal to it.}
  110.   CurExample:=ExampleHead;
  111.   While (CurExample<>Null) do
  112.      Begin
  113.        {Note in the following call, the first parameter is passed using
  114.         'Stay', while the second is passed using 'Clen'.  This is because
  115.         the first is a variable parameter, expected to be in a specific
  116.         place in memory, while the second is a value parameter and is
  117.         not changed at all.  Technically here it is safe to use 'Dirt'
  118.         in place of 'Stay' because LMin makes no calls to the virtual
  119.         memory system and therefore ExampleHead is guaranteed to remain
  120.         in the same real memory location until we return to this portion
  121.         of the program.}
  122.        LMin(ExamplePtrR(ExampleHead,Stay)^.RndNum,
  123.          ExamplePtrR(CurExample,Clen)^.RndNum);
  124.        Unstay(ExampleHead);
  125.        ExamplePtrR(CurExample,Dirt)^.RecNum:=CurRecordNum-
  126.          ExamplePtrR(CurExample,Clen)^.RecNum;                                {Reverse the order of the numbering.}
  127.        CurExample:=ExamplePtrR(CurExample,Clen)^.Next;
  128.      End;
  129.  
  130.   {Print and disassemble the linked list.}
  131.   CurExample:=ExampleHead;
  132.   While (CurExample<>Null) do
  133.      Begin
  134.        With ExamplePtrR(CurExample,Stay)^ do
  135.           Begin
  136.             WriteLn('Record number ',RecNum);
  137.             Write  ('Has random number ',RndNum);
  138.             WriteLn('        ',EnglishWord);
  139.             ExampleHead:=Next;
  140.           End;
  141.        Unstay(CurExample);
  142.        Depossess(CurExample,SizeOf(ExampleRec));
  143.        CurExample:=ExampleHead;
  144.      End;
  145.  
  146.   CloseBook;
  147.  
  148. eND.